package com.easy.util;


import org.apache.commons.codec.digest.DigestUtils;

public class DigestutilMd5 {
    private final static int countindex=5;
    private final static int saltIndex=1;
    private final static char[] items={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};
    public static String randomGigest(String pass){
        int randomCountIndex=(int)(Math.random()* items.length);
        int randomsaltIndex=(int)(Math.random()* items.length);
        char count=items[randomCountIndex];
        char salt=items[randomsaltIndex];
        String result=pass+salt;
        System.out.println(salt);
        System.out.println(count);
        for (int i=0;i<count;i++){
            result= DigestUtils.md5Hex(result);
        }
        System.out.println(result);
        //result就是加密之后的字符串
        result=result.substring(0,saltIndex)+salt+result.substring(saltIndex);
        result=result.substring(0,countindex)+count+result.substring(countindex);
        System.out.println(result);
        return result;
    }
    /**
     *   判断原文和密文是否匹配
     * @param pass 密码原文
     * @param digestpass  加密后的密码
     * @return
     */
    public static boolean decode(String pass,String digestpass){
        char salt=digestpass.charAt(saltIndex);
        char count=digestpass.charAt(countindex);
//开始加密
        String result=pass+salt;
        for (int i=0;i<count;i++){
            result=DigestUtils.md5Hex(result);
        }
        result=result.substring(0,saltIndex)+salt+result.substring(saltIndex);
        result=result.substring(0,countindex)+count+result.substring(countindex);
        return result.equals(digestpass);
    }
//    public static void main(String[] args) {
//        String a=randomGigest("123123");
//        System.out.println(a);
//        boolean b=  decode("123123","50c184053eedc2ddff84d9a7359c5a78a9");
//        System.out.println(b);
//    }
}